package com.xdf.ecommerce.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chanchaw
 * @create 2025-10-14 14:10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NacosClientService {
    private final DiscoveryClient discoveryClient;
    public List<ServiceInstance> getNacosClientInfo(String serviceId){
        /* 测试 Hystrix 由于超时导致服务降级时启用下面代码
        try{
            Thread.sleep(2000);
        }catch (Exception ex){
            log.error("睡眠2秒时出现异常！");
        } */
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        log.info("服务[" + serviceId + "]的实例数量：" + instances.size());

        return instances;


        // 测试 Hystrix 熔断时放开下面代码
        // throw new RuntimeException("为测试Hystrix熔断手动抛出的异常！");
    }
}
